home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
NOVA - For the NeXT Workstation
/
NOVA - For the NeXT Workstation.iso
/
SourceCode
/
OOP_Course
/
Labs
/
Stack
/
Solution
/
Stack.m
< prev
next >
Wrap
Text File
|
1992-12-19
|
1KB
|
59 lines
/*Objective_C Implementation of the Stack Class: Stack.m*/
//SOLUTION
#import <stdio.h>
#import "Stack.h"
@implementation Stack
//Initialize a Stack instance of floating point elements
-init
{
return [self initCount:0 elementSize:sizeof(float) description:"f"];
//return id of newly created Stack instance
}
//Add an element to the stack
-push: (float)aNumber
{
[self addElement: &aNumber];
return self;
}
//Remove an element from the stack
-(float)pop
{
float number;
number = [self top]; //return value at top of stack
//Empty stack returns zero
if (numElements != 0)
[self removeLastElement]; /* This also automatically closes the gap */
/* at the end */
return number;
}
-(float)top
{
if (numElements == 0) return 0.0;
else return *(float *)[self elementAt:(numElements-1)];
}
//Print the elements on the stack
-printStack
{
int i=0;
if (numElements == 0)
printf("Stack is empty.\n");
else {
printf("%i elements on stack, top to bottom:\n", numElements);
//numElements is an instance variable inherited from the Storage class
for(i=(numElements-1);i>=0;--i)
printf(" %f\n", *((float *)[self elementAt:i]));
}
return self;
}
@end